package concordia.comp6231.server;

import static concordia.comp6231.common.ApplicationConfig.GROUPMEMBERSHIP_SERVER_PORT;

import java.util.List;

import concordia.comp6231.boxOfficeService.MyServiceOperations;
import concordia.comp6231.common.ApplicationConfig;
import concordia.comp6231.rfmulticast.Message;
import concordia.comp6231.rfmulticast.RFIFOMulticast;
import concordia.comp6231.rfmulticast.ResultHandler;

public class BoxOfficeBroadcast implements MyServiceOperations {
	
	private int checkResult;

	@Override
	public int check(String showID) {
		List<String> recipients = ReplicaMember.currentInstance().getGrupMembershipManager().getMembers();
		RFIFOMulticast rfm = new RFIFOMulticast(recipients);
		Message msg = new Message();
		msg.put("msg_type", "replica_broadcast_request");
		msg.put("action_name", "check");
		msg.put("showID", showID);
		ResultHandler sucessHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for action check handeled successfully.");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
				BoxOfficeBroadcast.this.checkResult = Integer.parseInt(msg.get("check_result"));
			}
		};
		
		ResultHandler failureHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for action check failed:");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		rfm.send(ApplicationConfig.getAsInt(GROUPMEMBERSHIP_SERVER_PORT), msg, sucessHandler, failureHandler);
		return checkResult;
	}

	@Override
	public void reserve(String customerID, String showID, int numberOfTickets) {
		List<String> recipients = ReplicaMember.currentInstance().getGrupMembershipManager().getMembers();
		RFIFOMulticast rfm = new RFIFOMulticast(recipients);
		Message msg = new Message();
		msg.put("msg_type", "replica_broadcast_request");
		msg.put("action_name", "reserve");
		msg.put("customerID", customerID);
		msg.put("showID", showID);
		msg.put("numberOfTickets", new Integer(numberOfTickets).toString());
		ResultHandler sucessHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for action reserve handeled successfully.");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		ResultHandler failureHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for reserve check failed:");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		rfm.send(ApplicationConfig.getAsInt(GROUPMEMBERSHIP_SERVER_PORT), msg, sucessHandler, failureHandler);
	}

	@Override
	public void cancel(String customerID, String showID, int numberOfTickets) {
		List<String> recipients = ReplicaMember.currentInstance().getGrupMembershipManager().getMembers();
		RFIFOMulticast rfm = new RFIFOMulticast(recipients);
		Message msg = new Message();
		msg.put("msg_type", "replica_broadcast_request");
		msg.put("action_name", "cancel");
		msg.put("customerID", customerID);
		msg.put("showID", showID);
		msg.put("numberOfTickets", new Integer(numberOfTickets).toString());
		ResultHandler sucessHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for action cancel handeled successfully.");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		ResultHandler failureHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for cancel check failed:");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		rfm.send(ApplicationConfig.getAsInt(GROUPMEMBERSHIP_SERVER_PORT), msg, sucessHandler, failureHandler);
	}

	@Override
	public void exchange(String customerID, String reservedShowID, int reservedTickets, String desiredShowID, int desiredTickets) {
		List<String> recipients = ReplicaMember.currentInstance().getGrupMembershipManager().getMembers();
		RFIFOMulticast rfm = new RFIFOMulticast(recipients);
		Message msg = new Message();
		msg.put("msg_type", "replica_broadcast_request");
		msg.put("action_name", "exchange");
		msg.put("customerID", customerID);
		msg.put("reservedShowID", reservedShowID);
		msg.put("reservedTickets", new Integer(reservedTickets).toString());
		msg.put("desiredShowID", desiredShowID);
		msg.put("desiredTickets", new Integer(desiredTickets).toString());
		ResultHandler sucessHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for action exchange handeled successfully.");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		ResultHandler failureHandler = new ResultHandler() {
			@Override
			public void handleResult(Message msg) {
				System.out.println("INFO: Broadcast for exchange check failed:");
				System.out.println("INFO: Broadcast result msg: " + msg.toString());
			}
		};
		
		rfm.send(ApplicationConfig.getAsInt(GROUPMEMBERSHIP_SERVER_PORT), msg, sucessHandler, failureHandler);
	}

	@Override
	public String getHostname() {
		// TODO Auto-generated method stub
		return null;
	}

}
